/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.managed;

import beans.ejb.FantasyleagueFacade;
import beans.ejb.FantasyteamFacade;
import beans.ejb.FantasyuserFacade;
import entities.Fantasyuser;
import entities.Fantasyteam;
import entities.Fantasyleague;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author stumbledog
 */
@ManagedBean
@SessionScoped
public class CreateLeagueBean implements Serializable {

    private String leagueName;
    private String leagueNote;
    private int maxUser;
    private String friendName;
    private Fantasyleague league;
    private Fantasyteam team;
    private Fantasyuser user;
    private List<Fantasyuser> userList;
    private List<Fantasyuser> invitedList = new ArrayList<Fantasyuser>();
    private List<Fantasyuser> selectedFriend;

    public List<Fantasyuser> getSelectedFriend() {
        return selectedFriend;
    }

    public void setSelectedFriend(List<Fantasyuser> selectedFriend) {
        this.selectedFriend = selectedFriend;
    }

    public List<String> getInvitedList() {
        List<String> result = new ArrayList<String>();
        for(int i=0;i<invitedList.size();i++){
            result.add(invitedList.get(i).getEmail());
        }
        return result;
    }

    public void setInvitedList(List<Fantasyuser> invitedList) {
        this.invitedList = invitedList;
    }
    private HashMap<String, Fantasyuser> map = new HashMap<String, Fantasyuser>();

    public String getUserList() {
        userList = userFacade.getAllUserList();
        for (int i = 0; i < userList.size(); i++) {
            map.put(userList.get(i).getEmail(), userList.get(i));
        }

        Iterator it = map.keySet().iterator();
        String result = "";

        if (it.hasNext()) {
            result += it.next();
        }

        while (it.hasNext()) {
            result += ",";
            result += it.next();
        }
        return result;
    }

    public void setUserList(List<Fantasyuser> userList) {
        this.userList = userList;
    }
    @EJB
    private FantasyuserFacade userFacade;
    @EJB
    private FantasyleagueFacade leagueFacade;
    @EJB
    private FantasyteamFacade teamFacade;

    public CreateLeagueBean() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        user = ((AccountBean) request.getSession().getAttribute("accountBean")).getAccount();
        league = new Fantasyleague();
        team = new Fantasyteam();
    }

    public String getFriendName() {
        return friendName;
    }

    public void setFriendName(String friendName) {
        this.friendName = friendName;
    }

    public String getLeagueName() {
        return leagueName;
    }

    public void setLeagueName(String leagueName) {
        this.leagueName = leagueName;
    }

    public String getLeagueNote() {
        return leagueNote;
    }

    public void setLeagueNote(String leagueNote) {
        this.leagueNote = leagueNote;
    }

    public int getMaxUser() {
        return maxUser;
    }

    public void setMaxUser(int maxUser) {
        this.maxUser = maxUser;
    }

    public void searchFriendbyName() {
    }

    public void addFriend() {
        Fantasyuser add = map.get(friendName);
        invitedList.add(add);

    }

    public void removeFriend() {
        for(int i=0;i<selectedFriend.size();i++){
            Fantasyuser remove = map.get(selectedFriend.get(i));
            invitedList.remove(remove);
        }
    }

    public void createLeague() {
        league.setOwnerUserId(user);
        league.setMaximumNumberOfUsers(maxUser);
        league.setCurrentNumberOfUsers(invitedList.size() + 1);
        league.setLeagueName(leagueName);
        league.setLeagueNote(leagueNote);
        league.setDraftDate(new Date(2012, 12, 13));
        leagueFacade.createLeague(league);

        team.setLeagueId(league);
        team.setUserId(user);
        team.setName(user.getLoginName());
        teamFacade.create(team);
        

        for (int i = 0; i < invitedList.size(); i++) {
            Fantasyteam friendTeam = new Fantasyteam();
            friendTeam.setCurrentWin(0);
            friendTeam.setCurrentLose(0);
            friendTeam.setCurrentWithdraw(0);
            friendTeam.setLeagueId(league);
            friendTeam.setUserId(invitedList.get(i));
            teamFacade.create(friendTeam);
        }
    }
}
